package com.flashextended.apps.logomaker.view.windows
{
	import com.flashextended.apps.logomaker.beans.workareaobjects.SymbolOnStage;
	import com.flashextended.apps.logomaker.io.IOMediator;
	import com.flashextended.apps.logomaker.singleton.AppControl;
	import com.flashextended.apps.logomaker.singleton.AppLocales;
	import com.flashextended.apps.logomaker.view.windows.symbolpreview.SymbolPreview;
	import com.flashextended.utils.graphics.symbols.SymbolsBuilder;
	import com.flashextended.utils.graphics.windows.GenericInfoWindow;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Tile;
	import mx.controls.Alert;

	public class AddSymbolW extends GenericInfoWindow
	{
		private var containerTL:Tile;
		private var countResources:int=0;
		private var symbolsPreviewAC:ArrayCollection;
		
		public function AddSymbolW(w:Number, h:Number, title:String="Info")
		{
			super(w, h, title);
			symbolsPreviewAC = new ArrayCollection();
			this.getOkButton().enabled = false;
			buildContainer();
			//getSymbolsFromServer();//DEPRECHARED - OLD IMPLEMENTATION - symbols were retrived from the server
			buildSymbols();
		}
		
		/**
		 * build tile container to display symbols
		 */ 
		private function buildContainer():void
		{
			containerTL = new Tile();
			containerTL.percentWidth = 100;
			containerTL.percentHeight = 100;
			containerTL.setStyle("paddingLeft", 8);
			containerTL.setStyle("paddingTop", 8);
			containerTL.setStyle("paddingRight", 8);
			containerTL.setStyle("paddingBottom", 8);
			containerTL.setStyle("verticalGap", 8);
			containerTL.setStyle("horizontalGap", 8);
			this.getContentPane().addChild(containerTL);
		}
		
		private function buildSymbols():void
		{
				var starsInnerRadius:int = 20;
				for(var i:int=0;i<6;i++)
				{
					buildStars(starsInnerRadius);
					starsInnerRadius+=5;
				}
				var starsOuterRadius:int = 20;
				for(var j:int=0;j<6;j++)
				{
					buildBursts(starsOuterRadius);
					starsOuterRadius+=5;
				}
				starsOuterRadius = 50;
				var starsOuterInerRadius:int = 10;				
				buildBursts(starsOuterRadius, starsOuterInerRadius);
				for(var k:int=0;k<8;k++)
				{
					buildBursts(starsOuterRadius, starsOuterInerRadius);
					starsOuterInerRadius+=5;
				}
		}
			private function buildStars(innerRadius:int=20):void
			{
				var points:int = 4;
				for(var i:int=0;i<27;i++)
				{
					var sp:Sprite = new Sprite();
					SymbolsBuilder.buildStar(sp, 50, 50, points, innerRadius, 50);
					addSymbol(sp);
					points++;
				}
			}
			private function buildBursts(outerRadius:int=20, inerRadius:int=50):void
			{
				var points:int = 4;
				for(var i:int=0;i<27;i++)
				{
					var sp:Sprite = new Sprite();
					SymbolsBuilder.buildBurst(sp, 50, 50, points, inerRadius, outerRadius);
					addSymbol(sp);
					points++;
				}
			}		
		
		/**
		 * Add symbol to tile container
		 */ 
		public function addSymbol(l:Sprite):void
		{
			var item:SymbolPreview = new SymbolPreview(l);
			containerTL.addChild(item);
			symbolsPreviewAC.addItem(item);
			item.addEventListener(MouseEvent.CLICK, symbolMouseHandler);
			item.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickMouseHandler);
			(countResources==0)?item.isSelected = true:null;
			(item.isSelected)?this.getOkButton().enabled = true:null;
			countResources++;
			//displaySymbols();
		}
		
		/**
		 * retrive symbols from server IF firstTime
		 */ 
		protected function getSymbolsFromServer():void
		{
			(AppLocales.getInstance().sybmolsResources == null)?IOMediator.getInstance().broker.getSymbols(retriveSymbols):displaySymbols();
		}
		/**
		 * retrive symbols handler
		 */ 
		private function retriveSymbols(data:Object):void
		{
			if(data.status=="OK")
			{
				AppLocales.getInstance().sybmolsResources = data;
				displaySymbols();
			}else
			{
				Alert.show(data.error);
			}
		}
		
		private function displaySymbols():void
		{
			if(countResources < AppLocales.getInstance().sybmolsResources.resources.length)
			{
				IOMediator.getInstance().broker.readSymbol(addSymbol, AppLocales.getInstance().sybmolsResources.resources[countResources]);
			}
			//Alert.show("display"+AppLocales.getInstance().sybmolsResources.resources[0]);
			//IOMediator.getInstance().broker.readSymbol(addSymbol, 
		}
		
		private function symbolMouseHandler(e:MouseEvent):void
		{
			selectItem(SymbolPreview(e.currentTarget));
		}
		private function doubleClickMouseHandler(e:MouseEvent):void
		{
			selectItem(SymbolPreview(e.currentTarget));
			AppControl.getInstance().workarea.addObject(new SymbolOnStage(extractSelected()));
			this.cancelMouseHandler();					
		}
		
		public function selectItem(clickedItem:SymbolPreview):void
		{
			for(var i:int=0;i<symbolsPreviewAC.length;i++)
			{
				if(SymbolPreview(symbolsPreviewAC[i]) == clickedItem)
				{
					SymbolPreview(symbolsPreviewAC[i]).isSelected = true;
				}
				else
				{
					SymbolPreview(symbolsPreviewAC[i]).isSelected = false;
				}
			}			
		}
		
		override public function okMouseHandler(e:MouseEvent):void
		{
			//AppControl.getInstance().workarea.addObject(new TextOnStage("initial text"));
			AppControl.getInstance().workarea.addObject(new SymbolOnStage(extractSelected()));
			this.cancelMouseHandler();
		}
		
		private function extractSelected():Bitmap
		{
			var l:Sprite = null;
			for(var i:int=0;i<symbolsPreviewAC.length;i++)
			{
				if(SymbolPreview(symbolsPreviewAC[i]).isSelected)
				{
					l = SymbolPreview(symbolsPreviewAC[i]).getLoader();
					l.width = SymbolPreview(symbolsPreviewAC[i]).getOriginalW();
					l.height = SymbolPreview(symbolsPreviewAC[i]).getOriginalH();
				}
			}
			var bmd:BitmapData = new BitmapData(l.width, l.height, true, 1);
			bmd.draw(l);
			var bmp:Bitmap = new Bitmap(bmd);
			return bmp;		
		}
		
	}
}